/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DBLayer;
import ModelLayer.*;
import java.sql.*;
/**
 *
 * @author L.E.S & AMP
 */
public class DBProduct implements IFDBProduct {
	 
	private  Connection con;
    /** Creates a new instance of DBProduct */
    public DBProduct() 
    {
      con = DBConnection.getInstance().getDBcon();
    }

	public Product findProduct(String barcode, boolean retriveAssociation){

		DBClothing dbclothing = new DBClothing();
		DBGunReplicas dbgun = new DBGunReplicas();
		DBEquipment dbequip = new DBEquipment();
		Product p = null;
		if(dbclothing.findClothing(barcode, retriveAssociation)!= null){


			p = dbclothing.findClothing(barcode, retriveAssociation);
		}
		else if(dbgun.findGunReplicas(barcode, retriveAssociation)!= null){

			p = dbgun.findGunReplicas(barcode, retriveAssociation);
		}
		else if(dbequip.findEquipment(barcode, retriveAssociation) != null){

			p = dbequip.findEquipment(barcode, retriveAssociation);
		}
		return p;
		
	}

	public void decreaseCurrentStock (String barcode, int quantity) {

		Product p = findProduct(barcode, true);
		int curStock = p.getCurrentStock()-quantity;
		p.setCurrentStock(curStock);
		updateProduct(p);
	}

	public void increaseCurrentStock (String barcode, int quantity) {

		Product p = findProduct(barcode, true);
		int curStock = p.getCurrentStock()+ quantity;
		p.setCurrentStock(curStock);
		updateProduct(p);
	}
	public void increaseMinStock (String barcode, int quantity) {

		Product p = findProduct(barcode, true);
		int minStock = p.getMinStock()+ quantity;
		p.setMinStock(minStock);
		updateProduct(p);
	}
	public void decreaseMinStock (String barcode, int quantity) {

		Product p = findProduct(barcode, true);
		int minStock = p.getMinStock()+ quantity;
		p.setMinStock(minStock);
		updateProduct(p);
	}
	public void increaseMaxStock (String barcode, int quantity) {
		
		Product p = findProduct(barcode, true);
		int MaxStock = p.getMaxStock()+ quantity;
		p.setMaxStock(MaxStock);
		updateProduct(p);
	}
	public void decreaseMaxStock (String barcode, int quantity) {

		Product p = findProduct(barcode, true);
		int MaxStock = p.getMaxStock()+ quantity;
		p.setMaxStock(MaxStock);
		updateProduct(p);
	}

	public int updateProduct(Product product) {
            Product productObj  = product;
            int rc=-1;

            String query="UPDATE product SET "+
                      "currentStock ="+ productObj.getCurrentStock() +", "+
                      "minStock ="+ productObj.getMinStock() + ", " +
                      "maxStock ="+ productObj.getMaxStock() +
                      "WHERE barcode = '"+ productObj.getBarCode() + "'";
            System.out.println("Update query:" + query);
            try{ // update product
                    Statement stmt = con.createStatement();
                    stmt.setQueryTimeout(5);
                    rc = stmt.executeUpdate(query);

                    stmt.close();
            }//slut try
            catch(Exception ex){
                    System.out.println("Update exception in product db: "+ex);
            }
            return(rc);
    }

	//Singelwhere is used when we only select one customer
    private Product singleWhere(String wClause, boolean retriveAssociation)
    {
        ResultSet results;
        Product productObj = new Product();

        String query =  buildQuery(wClause);
        System.out.println(query);
        try{ // read the Product from the database
            Statement stmt = con.createStatement();
            stmt.setQueryTimeout(5);
            results = stmt.executeQuery(query);

            if( results.next() )
            {
                productObj = buildProduct(results);
                //assocaition is to be build
                stmt.close();

            }

        }//end try
        catch(Exception e){
            System.out.println("Query exception: "+e);
        }
        return productObj;
    }
    //method to build the query
    private String buildQuery(String wClause)
    {
			String query="SELECT minStock, currentStock, maxStock FROM product";

            if (wClause.length()>0)
                    query=query+" WHERE "+ wClause;

            return query;
    }
    //method to build an customer object
    private Product buildProduct(ResultSet results)
    {
        Product productObj = new Product();
        try{ // the columns from the table product  are used
            productObj.setCurrentStock(results.getInt("currentStock"));
            productObj.setMinStock(results.getInt("minStock"));
            productObj.setMaxStock(results.getInt("maxStock"));
        }
         catch(Exception e)
         {
         System.out.println("error in building the Product object");
         }
    return productObj;
    }
}
